package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.EasybuyOrder;
import com.ly.bean.EasybuyOrderDetail;
import com.ly.mapper.EasybuyOrderDetailMapper;
import com.ly.mapper.EasybuyOrderMapper;
import com.ly.service.OrderService;
import com.ly.util.ExChangeNameUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李岩
 * @date 2021/12/20-19:35
 * @describe
 */
@Component
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    private final Logger logger= LoggerFactory.getLogger(OrderServiceImpl.class);
    private OrderServiceImpl(){
        System.out.println("OrderServiceImpl   的service层");
    }
    @Autowired
    private EasybuyOrderMapper orderMapper;
    @Autowired
    private EasybuyOrderDetailMapper orderDetailMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 添加订单
     */
    public int insert(EasybuyOrder order, List<EasybuyOrderDetail> orderDetailList) throws Exception{
        logger.info("添加订单");
        //需要在mapper中使用主键回填获取order的id
        orderMapper.insert(order);
        for (EasybuyOrderDetail orderDetail : orderDetailList) {
            orderDetail.setOrderid(order.getId());
            System.out.println(orderDetail);
            orderDetailMapper.insertSelective(orderDetail);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("order",order);
        map.put("orderDetailList",orderDetailList);
        rabbitTemplate.convertAndSend(ExChangeNameUtil.FANOUT_EXCHANGE_ORDER,"",map);

        return order.getId();
    }
    /**
     * 修改订单表中支付的状态
     */
    @Override
    public void updateOrderPayStatus(Integer oid, String tradeId) throws Exception {
        EasybuyOrder order = orderMapper.selectByPrimaryKey(oid);
        order.setSerialnumber(tradeId);
        orderMapper.updateByPrimaryKeySelective(order);
    }
    //查询所有的订单信息
    @Override
    public PageInfo<EasybuyOrder> selectAllOrders(String pageNum) throws Exception {
        Integer pageNumInt=1;
        Integer pageSizeInt=5;
        if (pageNum!=null && !"".equals(pageNum)){
            pageNumInt=Integer.valueOf(pageNum);
        }
        PageHelper.startPage(pageNumInt,pageSizeInt);
        List<EasybuyOrder> list = orderMapper.selectAllOrders();
        PageInfo<EasybuyOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //根据登录的用户id获取该用户的订单信息
    @Override
    public PageInfo<EasybuyOrder> selectOrderByUserId(Integer userId,Integer pageNum) throws Exception {
        Integer pageNumInt=1;
        Integer pageSizeInt=6;
        if (pageNum!=null && !"".equals(pageNum)){
            pageNumInt=pageNum;
        }
        PageHelper.startPage(pageNumInt,pageSizeInt);
        List<EasybuyOrder> list = orderMapper.selectOrderByUserId(userId);
        PageInfo<EasybuyOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
